package com.hmy.finance.service.receipt.dto;

import cn.hutool.core.bean.BeanUtil;
import com.hmy.ccp.common.dto.ContextUserInfo;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.ccp.starter.web.utils.UserContextUtil;
import com.hmy.finance.api.bo.core.request.enums.ReceiptOffsetBillStatusEnum;
import com.hmy.finance.api.bo.core.request.receiptOffset.ReceiptOffsetAuditRequest;
import com.hmy.finance.api.enums.ReceiptOffsetApprovalNodeEnum;
import com.hmy.finance.dal.po.receipt.ReceiptOffsetBillPo;
import com.hmy.workflow.api.bo.request.task.ApprovalTaskRequest;
import java.io.Serializable;
import java.util.Map;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class ReceiptOffsetAduitDto implements Serializable {
    
    /**
     * 核销单id
     */
    private Long receiptOffsetBillId;

    /**
     * 是否通过:true:通过；false:不通过
     */
    private Boolean pass;

    /**
     * 审批备注
     */
    private String comment;

    /**
     * 当前任务id
     */
    private String processCurrentTaskId;

    /**
     * 当前审批节点
     */
    private String approvalNode;

    /**
     * 当前审批节点枚举
     */
    private ReceiptOffsetApprovalNodeEnum approvalNodeEnum;

    /**
     * 核销单
     */
    private ReceiptOffsetBillPo receiptOffsetBillPo;
    
    public static ReceiptOffsetAduitDto convertToDto(ReceiptOffsetAuditRequest request){
        ReceiptOffsetAduitDto aduitDto = BeanUtil.copyProperties(request, ReceiptOffsetAduitDto.class);
        aduitDto.setApprovalNodeEnum(ReceiptOffsetApprovalNodeEnum.getEnumByValue(aduitDto.getApprovalNode()));
        return aduitDto;
    }

    public ReceiptOffsetBillPo buildUpdateReceiptOffsetBillPo() {
        ReceiptOffsetBillStatusEnum toStatusEnum;
        switch (approvalNodeEnum) {
            case AREA_MANAGER:
                toStatusEnum = ReceiptOffsetBillStatusEnum.CASHIER_APPROVE_ING;
                break;
            case CASHIER:
                toStatusEnum = ReceiptOffsetBillStatusEnum.ARACCOUNTANT_APPROVE_ING;
                break;
            case ARACCOUNTANT:
                toStatusEnum = ReceiptOffsetBillStatusEnum.APPROVE_PASS;
                break;
            default:
                throw new HMYRuntimeException(506900009);
        }
        
        //审批通过
        if (Boolean.TRUE.equals(pass)) {
            receiptOffsetBillPo.setBillStatus(toStatusEnum.getStatusCode());
        } else {
            //审核驳回
            receiptOffsetBillPo.setBillStatus(ReceiptOffsetBillStatusEnum.APPROVE_REJECTED.getStatusCode());
        }
        ContextUserInfo userInfo = UserContextUtil.getCurrentUser();
        receiptOffsetBillPo.setUpdaterId(userInfo.getId());
        receiptOffsetBillPo.setUpdaterName(userInfo.getUserName());
        
        return receiptOffsetBillPo;
    }

    public ApprovalTaskRequest buildCompleteRequest(ReceiptOffsetBillStatusEnum toBillStatusEnum) {
        ApprovalTaskRequest approvalTaskRequest = new ApprovalTaskRequest();
        approvalTaskRequest.setTaskId(processCurrentTaskId);
        approvalTaskRequest.setComment(StringUtils.isEmpty(comment) ? "通过" : comment);
        approvalTaskRequest.setProcessInstanceId(receiptOffsetBillPo.getProcessInstanceId());
        ReceiptOffsetBillProcessVariableDto variableDto = BeanUtil.copyProperties(receiptOffsetBillPo, ReceiptOffsetBillProcessVariableDto.class);
        ContextUserInfo userInfo = UserContextUtil.getCurrentUser();
        variableDto.setUpdaterId(userInfo.getId());
        variableDto.setUpdaterName(userInfo.getUserName());
        variableDto.buildReceiptOffsetBillStatus(toBillStatusEnum);

        Map<String, Object> variableMap = BeanUtil.beanToMap(variableDto);
        
        switch (approvalNodeEnum) {
            case AREA_MANAGER:
                variableMap.put("areaManagerAuditResult", pass ? "agree" : "reject");
                //下一个审批节点
                variableMap.put("approvalNode", ReceiptOffsetApprovalNodeEnum.CASHIER.getCode());
                break;
            case CASHIER:
                variableMap.put("cashierAuditResult", pass ? "agree" : "reject");
                //下一个审批节点
                variableMap.put("approvalNode", ReceiptOffsetApprovalNodeEnum.ARACCOUNTANT.getCode());
                break;
            case ARACCOUNTANT:
                variableMap.put("arAccountantAuditResult", pass ? "agree" : "reject");
                break;
            default:
                throw new HMYRuntimeException(506900009);
        }

        if (Boolean.FALSE.equals(pass)) {
            variableMap.put("rejectCount", 1);
        }
        approvalTaskRequest.setVariables(variableMap);
        return approvalTaskRequest;
    }
}
